package top.yuhaitao.core._config.mybatis;


import com.github.pagehelper.PageInterceptor;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;


@SpringBootConfiguration
//@PropertySource("application-mybatis.yml")
public class MyBatisConfig {

    private static final Logger logger = LoggerFactory.getLogger(MyBatisConfig.class);


    @Value("${spring.datasource.driver-class-name}")
    private String jdbcDriver;
    @Value("${spring.datasource.url}")
    private String jdbcUrl;
    @Value("${spring.datasource.username}")
    private String jdbcUser;
    @Value("${spring.datasource.password}")
    private String jdbcPassword;


    //    @Value("${mybatis.config.path}")
//    private String myBatisConfigPath;
    @Value("${mybatis.mapper-locations}")
    private String mapperXMLConfigPath;
//    @Value("${mybatis.type-aliases-package}")
//    private String mapperPackagePath;

    @Bean(name = "dataSource")
    public DataSource createDataSource() {

        logger.info("--------------------------------");
        logger.info("|MyBatisConfig.createDataSource|");
        logger.info("--------------------------------");

        //实例化类
        HikariConfig hikariConfig = new HikariConfig();
        //驱动类型
        hikariConfig.setDriverClassName(jdbcDriver);
        //设置url
        hikariConfig.setJdbcUrl(jdbcUrl);
        //数据库帐号
        hikariConfig.setUsername(jdbcUser);
        //数据库密码
        hikariConfig.setPassword(jdbcPassword);
        hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
        hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
        hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        return new HikariDataSource(hikariConfig);
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory createSqlSessionFactory(DataSource dataSource) throws Exception {
        logger.info("-------------------------------------");
        logger.info("|MyBatisConfig.sqlSessionFactory|");
        logger.info("------------------------------------- ");

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        String packageXMLConfigPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperXMLConfigPath;

        // 设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
//        设置MyBatis 配置文件的路径
//        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(myBatisConfigPath));
//        设置mapper 对应的XML 文件的路径
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageXMLConfigPath));
//         设置mapper 接口所在的包
//        sqlSessionFactoryBean.setTypeAliasesPackage(mapperPackagePath);

        // 设置MyBatis分页插件
        PageInterceptor pageInterceptor = this.initPageInterceptor();
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptor});
        return sqlSessionFactoryBean.getObject();
    }

    private PageInterceptor initPageInterceptor() {
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        pageInterceptor.setProperties(properties);
        return pageInterceptor;
    }


}
